package com.goodfox.web.generic.log.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.goodfox.web.generic.account.entity.Account;
import com.goodfox.web.generic.authority.service.PowerService;
import com.goodfox.web.generic.log.entity.OperatorLog;
import com.goodfox.web.generic.log.service.LogService;
import com.goodfox.web.generic.login.entity.LoginLog;
import com.goodfox.web.generic.provide.entity.Pager;
import com.goodfox.web.generic.provide.service.EntityService;
import com.goodfox.web.generic.provide.util.DateUtil;
import com.goodfox.web.generic.provide.util.ParameterUtil;

/**
 * 操作日志Controller
 * @author qin gt
 *
 */
@Controller
@RequestMapping(value="/log")
public class LogController {
	
	/** log4j日志器 */
	private Logger logger=Logger.getLogger(LogController.class);
	
	/** 日志Service */
	@Autowired
	private LogService logService;

	/** 权限Service */
	@Autowired
	private PowerService powerService;
	
	/** 实体Service */
	@Autowired
	private EntityService entityService;
	
	/**
	 * 进入登录日志查询页面
	 * @param request
	 * @param model
	 * @return 目标页面
	 */
	@RequestMapping(value="goLoginLogIndex.shtml")
	public String goLoginLogIndex(HttpServletRequest request,ModelMap model){
		logger.info("进入登录日志查询页面");
	    Account loginAccount=powerService.havePower(request);
	    if(loginAccount!=null){
			int currentPage=1;
			if(!ParameterUtil.getReqParaValue(request,"pageNum").equals("")){
				currentPage=Integer.parseInt(ParameterUtil.getReqParaValue(request,"pageNum"));
			}
			int numPerPage=15;
			if(!ParameterUtil.getReqParaValue(request,"numPerPage").equals("")){
				numPerPage=Integer.parseInt(ParameterUtil.getReqParaValue(request,"numPerPage"));
			}
		    Map queryMap=entityService.fillQueryMap(request,model);
		    if(queryMap.get("lgnTimeStart")!=null && !String.valueOf(queryMap.get("lgnTimeStart")).equals("")){
			    queryMap.put("lgnTimeStart",DateUtil.dateFormat(String.valueOf(queryMap.get("lgnTimeStart")),"yyyy-MM-dd HH:mm:ss"));
		    }
		    if(queryMap.get("lgnTimeEnd")!=null && !String.valueOf(queryMap.get("lgnTimeEnd")).equals("")){
			    queryMap.put("lgnTimeEnd",DateUtil.dateFormat(String.valueOf(queryMap.get("lgnTimeEnd")),"yyyy-MM-dd HH:mm:ss"));
		    }
		    
			Pager<LoginLog> pager=logService.queryLoginLogPager(currentPage, numPerPage, queryMap);
		    model.put("loginList", pager.getEntityList());
			
			model.put("totalCount", pager.getAllCount());
			model.put("currentPage", currentPage);
			model.put("numPerPage", numPerPage);
			
	    	return "log/loginLogIndex";
	    }else{
	    	return "redirect:/login/logout.shtml";
	    }
	}
	
	/**
	 * 进入操作日志页面
	 * @param request
	 * @param model
	 * @return  目标页面
	 */
	@RequestMapping(value="goOperLogIndex.shtml")
	public String goOperLogIndex(HttpServletRequest request,ModelMap model){
		logger.info("进入操作日志查询页面");
	    Account loginAccount=powerService.havePower(request);
	    if(loginAccount!=null){
			int currentPage=1;
			if(!ParameterUtil.getReqParaValue(request,"pageNum").equals("")){
				currentPage=Integer.parseInt(ParameterUtil.getReqParaValue(request,"pageNum"));
			}
			int numPerPage=15;
			if(!ParameterUtil.getReqParaValue(request,"numPerPage").equals("")){
				numPerPage=Integer.parseInt(ParameterUtil.getReqParaValue(request,"numPerPage"));
			}
		    Map queryMap=entityService.fillQueryMap(request,model);
			Pager<OperatorLog> pager=logService.queryOperatorLogPager(currentPage, numPerPage, queryMap);
		    model.put("operList", pager.getEntityList());
			
			model.put("totalCount", pager.getAllCount());
			model.put("currentPage", currentPage);
			model.put("numPerPage", numPerPage);
			
			model.put("loginLog", logService.queryLoginLog(queryMap.get("lgnLogId").toString()));
			return "log/operLogIndex";
	    }else{
	    	return "redirect:/login/logout.shtml";
	    }
	}
	
	/**
	 * 进入操作日志查看详情页面 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="goOperLogDetail.shtml")
	public String goOperLogDetail(HttpServletRequest request,ModelMap model){
		logger.info("进入操作日志查看详情页面");
	    Account loginAccount=powerService.havePower(request);
	    if(loginAccount!=null){
		    logger.info("opeLogId:"+ParameterUtil.getReqParaValue(request, "opeLogId"));
			OperatorLog operatorLog=logService.getOperatorLog(ParameterUtil.getReqParaValue(request, "opeLogId"));
			model.put("oper",operatorLog);
			return "log/operLogDetail";
	    }else{
	    	return "redirect:/login/logout.shtml";
	    }
	}

}
